<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="仓库编号">
          <a-input placeholder="请输入仓库编号" v-decorator="['warehouseCode', validatorRules.warehouseCode]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="仓库名称">
          <a-input placeholder="请输入仓库名称" v-decorator="['warehouseName', validatorRules.warehouseName]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="仓库类型">
          <j-dict-select-tag type="list" v-decorator="['warehouseType', validatorRules.warehouseType]" :trigger-change="true" dictCode="wms_warehouse_type" placeholder="请选择仓库类型" @change="warehouseTypeChange"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否需要库位绑定">
          <j-dict-select-tag type="list" v-decorator="['needLocationBind', validatorRules.needLocationBind]" dictCode="yn" placeholder="请选择是否需要库位绑定"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="仓库地址">
          <a-input placeholder="请输入仓库地址" v-decorator="['address']" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="负责人">
          <j-select-employee-modal
            v-decorator="['manager']"
            :multiple="false"
            departName="仓储部"
            placeholder="请选择负责人"
            @change="onManagerChange"
          />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="联系电话">
          <a-input placeholder="请输入联系电话" v-decorator="['phone']" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <j-dict-select-tag type="list" v-decorator="['status', validatorRules.status]" dictCode="wms_warehouse_status" placeholder="请选择状态"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-textarea placeholder="请输入备注" v-decorator="['remark']" :rows="4" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JSelectEmployeeModal from '@/components/jeecgbiz/JSelectEmployeeModal'
  
  export default {
    name: 'WarehouseModal',
    components: {
      JDictSelectTag,
      JSelectEmployeeModal
    },
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          warehouseCode: {
            rules: [
              { required: true, message: '请输入仓库编号!' }
            ]
          },
          warehouseName: {
            rules: [
              { required: true, message: '请输入仓库名称!' }
            ]
          },
          warehouseType: {
            rules: [
              { required: true, message: '请选择仓库类型!' }
            ]
          },
          needLocationBind: {
            rules: [
              { required: true, message: '请选择是否需要库位绑定!' }
            ]
          },
          status: {
            rules: [
              { required: true, message: '请选择状态!' }
            ]
          }
        },
        url: {
          add: '/wms/warehouse/add',
          edit: '/wms/warehouse/edit'
        },
        selectedManager: null
      }
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'warehouseCode', 'warehouseName', 'warehouseType', 'locationCount', 'address', 'manager', 'phone', 'status', 'remark'))
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      onManagerChange(value, selectedEmployees) {
        if (selectedEmployees && selectedEmployees.length > 0) {
          this.selectedManager = selectedEmployees[0];
          // 如果有电话号码，自动填充到联系电话字段
          if (this.selectedManager.phone) {
            this.form.setFieldsValue({
              'phone': this.selectedManager.phone
            });
          }
        }
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpUrl = '';
            let method = '';
            if (!this.model.id) {
              httpUrl = this.url.add;
              method = 'post';
            } else {
              httpUrl = this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            httpAction(httpUrl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      warehouseTypeChange(value) {
        // 根据仓库类型设置是否需要库位绑定
        if (value) {
          // 原材料、半成品、成品仓库需要库位绑定
          if (value === '1' || value === '2' || value === '3') {
            this.form.setFieldsValue({ needLocationBind: '1' }); // 1-需要
          }
          // 辅料仓库建议库位绑定
          else if (value === '4') {
            this.form.setFieldsValue({ needLocationBind: '1' }); // 1-需要
          }
          // 废料仓库可选库位绑定
          else if (value === '5') {
            this.form.setFieldsValue({ needLocationBind: '0' }); // 0-不需要
          }
          // 默认需要绑定
          else {
            this.form.setFieldsValue({ needLocationBind: '1' }); // 1-需要
          }
        }
      }
    }
  }
</script> 